package com.campusnews.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campusnews.annotation.LoginToken;
import com.campusnews.pojo.*;
import com.campusnews.service.InvitationService;
import com.campusnews.service.TopicService;
import com.campusnews.utils.EntryToMapUtils;
import com.campusnews.utils.JsonResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2021-09-26
 */
@RestController
@ApiOperation(value = "话题API", notes = "话题API")
@RequestMapping("/topic")
@Api(tags={"话题API"})
public class TopicController {
    @Autowired
    private TopicService topicService;

    @Autowired
    private InvitationService invitationService;

    @LoginToken
    @ApiOperation(value = "添加话题信息", notes = "添加话题信息")
    @RequestMapping(value = "/insertTopic" ,method = RequestMethod.POST)
    public JsonResult insertTopic(@RequestBody Topic topic) {
        QueryWrapper<Topic> wrapper = new QueryWrapper<>();
        wrapper.eq("topic_name",topic.getTopicName());
        Topic topicBase = topicService.getOne(wrapper);
        if (topicBase!=null){
            return JsonResult.errorMsg("话题已存在");
        }else {
            boolean save = topicService.save(topic);
            if (save){
                System.out.println(topic);
                return JsonResult.build(200,"话题添加成功",save);
            }else {
                return JsonResult.errorMsg("话题添加失败");
            }
        }
    }

//    @LoginToken
//    @ApiOperation(value = "修改话题信息", notes = "修改话题信息")
//    @RequestMapping(value = "/updateTopic" ,method = RequestMethod.POST)
//    public JsonResult updateTopic(@RequestBody Topic topic) {
//        boolean update = topicService.saveOrUpdate(topic);
//        if (update){
//            return JsonResult.build(200,"话题修改成功",update);
//        }else {
//            return JsonResult.errorMsg("话题修改失败");
//        }
//    }

    @LoginToken
    @ApiOperation(value = "删除话题信息", notes = "删除话题信息")
    @RequestMapping(value = "/deleteTopic/{topicId}" ,method = RequestMethod.GET)
    public JsonResult deleteTopic(@PathVariable("topicId") int topicId) {
        QueryWrapper<Topic> wrapper = new QueryWrapper<>();
        wrapper.eq("topic_id",topicId);
        boolean remove = topicService.remove(wrapper);
        QueryWrapper<Invitation> wrapper1 = new QueryWrapper<>();
        wrapper1.like("topic_list",topicId);
        List<Invitation> invitationList = invitationService.list(wrapper1);
        for (Invitation invitation : invitationList) {
            String topicList = invitation.getTopicList();
            String replace = topicList.replace(topicId + ",", "");
            invitation.setTopicList(replace);
            invitationService.updateInvite(invitation);
        }
        if (remove){
            return JsonResult.build(200,"话题删除成功",remove);
        }else {
            return JsonResult.errorMsg("话题删除失败");
        }
    }

    @LoginToken
    @ApiOperation(value = "分页精确查询话题信息", notes = "分页精确话题信息")
    @RequestMapping(value = "/selectTopicByAccurate" ,method = RequestMethod.POST)
    public JsonResult selectTopicByAccurate(@RequestBody Topic topic) {
        Map<String, Object> map = new HashMap<>();
        //实体类转化为map集合
        try {
            map = EntryToMapUtils.entryToMap(topic);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //条件查询器
        QueryWrapper<Topic> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);
        if (!(topic.getSortOrder()==null||topic.getSortOrder().length()==0)){
            wrapper.orderByDesc(topic.getSortOrder());
        }
        PageHelper.startPage(topic.getPage(),topic.getPageSize());
        PageInfo<Topic> pageInfo = new PageInfo<>((Page<Topic>)topicService.list(wrapper));
        System.out.println(pageInfo);
        return JsonResult.build(200,"分页精确话题信息查询成功",pageInfo);
    }

    @LoginToken
    @ApiOperation(value = "分页模糊查询话题信息", notes = "分页模糊话题信息")
    @RequestMapping(value = "/selectTopicByVague" ,method = RequestMethod.POST)
    public JsonResult selectTopicByVague(@RequestBody Thing things) {
        //条件查询器
        QueryWrapper<Topic> wrapper = new QueryWrapper<>();
        String thing = things.getThing();
        if (!(thing==null||thing.length()==0)){
            wrapper.like("topic_name",thing).or()
                    .like("topic_descr",thing).or()
//                    .like("topic_icon",thing).or()
//                    .like("invi_num",Integer.valueOf(thing))
            ;
        }
        if (!(things.getSortOrder()==null||things.getSortOrder().length()==0)){
            wrapper.orderByDesc(things.getSortOrder());
        }
        PageHelper.startPage(things.getPage(),things.getPageSize());
        PageInfo<Topic> pageInfo = new PageInfo<>((Page<Topic>)topicService.list(wrapper));
        System.out.println(pageInfo);
        return JsonResult.build(200,"分页模糊话题信息查询成功",pageInfo);
    }


    @LoginToken
    @ApiOperation(value = "分页查看该话题的所有帖子", notes = "分页查看该话题的所有帖子")
    @RequestMapping(value = "/selectInviteByTopic" ,method = RequestMethod.POST)
    public JsonResult selectInviteByTopic(@RequestBody Thing things) {
        PageInfo<Invitation> pageInfo = invitationService.selectInviteByTopic(things);
        return JsonResult.build(200,"分页模糊话题信息查询成功",pageInfo);
    }

}

